﻿using Md.Api.Entity.DapperModels.MdDB;

namespace Md.Dapper.Repositories.Md;
/// <summary>
/// 系统用户仓储
/// </summary>
public class SysUserRep : BaseRep<SysUserModel>
{
    /// <summary>
    /// 字段
    /// </summary>
    private static string sqlFields = string.Format(@"
         `user_id`
        ,`dept_id`
        ,`user_name`
        ,`nick_name`
        ,`user_type`
        ,`email`
        ,`phonenumber`
        ,`sex`
        ,`avatar`
        ,`status`
        ,`del_flag`
        ,`login_ip`
        ,`login_date`
        ,`create_by`
        ,`create_time`
        ,`update_by`
        ,`update_time`
        ,`remark`
    ");

    /// <summary>
    /// 用户登录
    /// </summary>
    /// <param name="userName">用户名</param>
    /// <param name="userPwd">用户密码</param>
    /// <returns>SysUserModel</returns>
    public SysUserModel? Login(string? userName, string? userPwd)
    {
        string sqlWhere = string.Format(@" and `del_flag` = '0' and `user_name` = @UserName and `password` = @Password ");

        var res = QuerySingle(sqlFields, sqlWhere, new { UserName = userName, Password = userPwd });

        return res;
    }

    /// <summary>
    /// 通过用户编号获取系统用户信息
    /// </summary>
    /// <param name="userId">用户编号</param>
    /// <returns>SysUserModel</returns>
    public SysUserModel? GetSysUserByUserId(long userId)
    {
        string sqlWhere = string.Format(@" and `del_flag` = '0' and `user_id` = @UserId ");

        return QuerySingle(sqlFields, sqlWhere, new { UserId = userId });
    }

    /// <summary>
    /// 通过用户编号获取系统用户密码
    /// </summary>
    /// <param name="userId">用户编号</param>
    /// <returns>SysUserModel</returns>
    public SysUserModel? GetSysUserPwdByUserId(long userId)
    {
        string sqlFields = string.Format(@" `password`, `del_flag`, `status` ");

        string sqlWhere = string.Format(@" and `del_flag` = '0' and `user_id` = @UserId ");

        return QuerySingle(sqlFields, sqlWhere, new { UserId = userId });
    }

    /// <summary>
    /// 获取系统用户列表
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <returns>IEnumerable<SysUserModel></returns>
    public IEnumerable<SysUserModel> GetSysUserList(SysUserModel model, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (model.DeptId > 0)
        {
            sqlWhere += " and `dept_id` = @DeptId";
        }
        if (!string.IsNullOrWhiteSpace(model.UserName))
        {
            sqlWhere += " and `user_name` like concat('%', @UserName, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.NickName))
        {
            sqlWhere += " and `nick_name` like concat('%', @NickName, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.UserType))
        {
            sqlWhere += " and `user_type` in (@UserType) ";
        }
        if (!string.IsNullOrWhiteSpace(model.Email))
        {
            sqlWhere += " and `email` like concat('%', @Email, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Phonenumber))
        {
            sqlWhere += " and `phonenumber` like concat('%', @Phonenumber, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Sex))
        {
            sqlWhere += " and `sex` = @Sex ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }

        sqlWhere += " and `del_flag` = '0' ";

        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryList(sqlFields, sqlWhere, model);
    }

    /// <summary>
    /// 分页获取系统用户列表数据
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageSize">每页条数</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <returns>Page<SysUserModel></returns>
    public Page<SysUserModel> GetSysUserPageList(SysUserModel model, int pageIndex, int pageSize, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (model.DeptId > 0)
        {
            sqlWhere += " and `dept_id` = @DeptId";
        }
        if (!string.IsNullOrWhiteSpace(model.UserName))
        {
            sqlWhere += " and `user_name` like concat('%', @UserName, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.NickName))
        {
            sqlWhere += " and `nick_name` like concat('%', @NickName, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.UserType))
        {
            sqlWhere += " and `user_type` in (@UserType) ";
        }
        if (!string.IsNullOrWhiteSpace(model.Email))
        {
            sqlWhere += " and `email` like concat('%', @Email, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Phonenumber))
        {
            sqlWhere += " and `phonenumber` like concat('%', @Phonenumber, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.Sex))
        {
            sqlWhere += " and `sex` = @Sex ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }

        sqlWhere += " and `del_flag` = '0' ";

        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryPage(sqlFields, sqlWhere, model, pageIndex, pageSize);
    }

    /// <summary>
    /// 通过用户编号更新系统用户
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>long</returns>
    public long UpdateSysUserByUserId(SysUserModel model)
    {
        var sqlSet = string.Empty;

        #region ===set===
        if (model.DeptId > 0)
        {
            sqlSet += " `dept_id` = @DeptId, ";
        }

        if (!string.IsNullOrWhiteSpace(model.UserName))
        {
            sqlSet += " `user_name` = @UserName, ";
        }

        if (!string.IsNullOrWhiteSpace(model.NickName))
        {
            sqlSet += " `nick_name` = @NickName, ";
        }

        if (!string.IsNullOrWhiteSpace(model.UserType))
        {
            sqlSet += " `user_type` = @UserType, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Email))
        {
            sqlSet += " `email` = @Email, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Phonenumber))
        {
            sqlSet += " `phonenumber` = @Phonenumber, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Sex))
        {
            sqlSet += " `sex` = @Sex, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Avatar))
        {
            sqlSet += " `avatar` = @Avatar, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlSet += " `status` = @Status, ";
        }

        if (model.Remark != null)
        {
            sqlSet += " `remark` = @Remark, ";
        }

        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlSet += " `update_by` = @UpdateBy, ";
        }

        sqlSet += " `update_time` = sysdate() ";
        #endregion

        string sqlWhere = " and `user_Id` = @UserId ";

        return Update(sqlSet, sqlWhere, model);
    }

    /// <summary>
    /// 通过用户编号更新系统用户密码
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>long</returns>
    public long UpdateSysUserPwdByUserId(long userId, string userPwd)
    {
        string sqlSet = " `password` = @Password ";

        string sqlWhere = " and `user_Id` = @UserId ";

        return Update(sqlSet, sqlWhere, new { UserId = userId, Password = userPwd });
    }

    /// <summary>
    /// 通过系统用户编号删除系统用户
    /// </summary>
    /// <param name="userIds">系统用户编号多个逗号分隔</param>
    /// <returns>int</returns>
    public int DeleteSysUserByUserId(string userIds)
    {
        string sqlWhere = " and `user_id` in @UserIds ";

        return Delete(sqlWhere, new { UserIds = userIds.Split(',').ToArray() });
    }

    /// <summary>
    /// 检查系统用户名是否唯一
    /// </summary>
    /// <param name="userName">用户名</param>
    /// <returns>object?</returns>
    public object? CheckUserNameUnique(string userName)
    {
        string sqlField = " `user_id` ";

        string sqlWhere = " and `del_flag` = '0' and `user_name` = @UserName ";

        return Scalar(sqlField, sqlWhere, new { UserName = userName }); ;
    }

    /// <summary>
    /// 检查系统用户手机号是否唯一
    /// </summary>
    /// <param name="phone">手机号</param>
    /// <returns>object?</returns>
    public object? CheckPhoneUnique(string phone)
    {
        string sqlField = " `user_id` ";

        string sqlWhere = " and `del_flag` = '0' and `phonenumber` = @Phonenumber ";

        return Scalar(sqlField, sqlWhere, new { Phonenumber = phone });
    }

    /// <summary>
    /// 检查系统用户邮箱是否唯一
    /// </summary>
    /// <param name="email">邮箱</param>
    /// <returns>object?</returns>
    public object? CheckEmailUnique(string email)
    {
        string sqlField = " `user_id` ";

        string sqlWhere = " and `del_flag` = '0' and `email` = @Email ";

        return Scalar(sqlField, sqlWhere, new { Email = email });
    }
}

